package au.edu.utas.yu_qiao_group.luckyman.ui.recordlist;

import android.content.Context;
import android.content.res.Resources;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import au.edu.utas.yu_qiao_group.luckyman.R;
import au.edu.utas.yu_qiao_group.luckyman.databinding.TicketListItemBinding;
import au.edu.utas.yu_qiao_group.luckyman.db.tickets.TicketDTO;
import au.edu.utas.yu_qiao_group.luckyman.ui.components.SwipeRecyclerView;

public class RecordListViewItemAdapter extends SwipeRecyclerView.Adapter<RecordListViewItemAdapter.ViewHolder> {
  private static final String TAG = "RecordListViewItem";

  private List<TicketDTO> mDataSet;
  private Context mCtx;
  private View.OnClickListener mEditBtnListener, mDelBtnListener;

  public static class ViewHolder extends SwipeRecyclerView.ViewHolder implements SwipeRecyclerView.SwiperViewHolder {
    private TicketListItemBinding mBinding;
    public LinearLayout listRow, listHidden;
    public ImageView mImgView;
    public ImageButton mEditBtn;
    public TextView mPhone, mItemDelete;

    public ViewHolder(View itemView) {
      super(itemView);
      mBinding = DataBindingUtil.bind(itemView);
      listRow = itemView.findViewById(R.id.ticket_list_row);
      mPhone = itemView.findViewById(R.id.ticket_phone);
      listHidden = itemView.findViewById(R.id.list_hidden);
      mImgView = itemView.findViewById(R.id.raffle_cover);
      mEditBtn = itemView.findViewById(R.id.edit_btn);
      mItemDelete = itemView.findViewById(R.id.ticket_btn_del2);
    }

    public LinearLayout getListRow() {
      return listRow;
    }

    public LinearLayout getListHidden() {
      return listHidden;
    }

    public void bind(TicketDTO ticket) {
      mBinding.setTicket(ticket);
    }
  }

  /**
   * Initialize the dataset of the Adapter.
   *
   * @param dataSet String[] containing the data to populate views to be used by RecyclerView.
   */
  public RecordListViewItemAdapter(List<TicketDTO> dataSet, View.OnClickListener editBtnListener, View.OnClickListener delBtnListener) {
    mDataSet = dataSet;
    mDelBtnListener = delBtnListener;
    mEditBtnListener = editBtnListener;
  }

  // Create new views (invoked by the layout manager)
  @Override
  public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
    // Create a new view.
    View v = LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.ticket_list_item, viewGroup, false);
    mCtx = viewGroup.getContext();
    return new ViewHolder(v);
  }

  @Override
  public void onBindViewHolder(ViewHolder viewHolder, final int position) {
    Log.d(TAG, "Element " + position + " set.");
    TicketDTO t = mDataSet.get(position);
    viewHolder.bind(t);
    viewHolder.mPhone.setTextColor((t.getMJackpotFlag() == 0 ) ? ContextCompat.getColor(mCtx, R.color.colorSubAccent) : ContextCompat.getColor(mCtx, R.color.colorPrimary));
    viewHolder.mItemDelete.setOnClickListener(mDelBtnListener);
    viewHolder.mItemDelete.setTag(position);
    viewHolder.mEditBtn.setOnClickListener(mEditBtnListener);
    viewHolder.mEditBtn.setTag(position);
  }

  @Override
  public int getItemCount() {
    return mDataSet.size();
  }
}
